<?php

namespace app\admin\model\cms;

use addons\cms\library\FulltextSearch;
use addons\cms\library\IntCode;
use addons\cms\library\Service;
use addons\cms\model\Collection;
use app\common\model\cms\ArchivesInteraction;
use app\common\model\User;
use think\Model;
use traits\model\SoftDelete;

class Archives extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'cms_archives';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [
        'flag_text',
        'status_text',
        'publishtime_text',
//        'url',
//        'fullurl',
        'style_bold',
        'style_color',
    ];
    protected static $config = [];

    /**
     * 获取加密后的ID
     * @param $value
     * @param $data
     * @return string
     */
    public function getEidAttr($value, $data)
    {
        $value = $data['id'];
        if (self::$config['archiveshashids']) {
            $value = IntCode::encode($value);
        }
        return $value;
    }

//    public function getUrlAttr($value, $data)
//    {
//        return $this->buildUrl($value, $data);
//    }

//    public function getFullurlAttr($value, $data)
//    {
//        return $this->buildUrl($value, $data, true);
//    }

//    private function buildUrl($value, $data, $domain = false)
//    {
//        $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
//        $catename = isset($this->channel) && $this->channel ? $this->channel->diyname : 'all';
//        $cateid = isset($this->channel) && $this->channel ? $this->channel->id : 0;
//        $time = $data['publishtime'] ?? time();
//        $vars = [
//            ':id'       => $data['id'],
//            ':eid'      => $this->getEidAttr($data['id'], $data),
//            ':diyname'  => $diyname,
//            ':channel'  => $data['channel_id'],
//            ':catename' => $catename,
//            ':cateid'   => $cateid,
//            ':year'     => date("Y", $time),
//            ':month'    => date("m", $time),
//            ':day'      => date("d", $time),
//        ];
//        return addon_url('cms/archives/index', $vars, static::$config['urlsuffix'], true);
//    }

    public function getOriginData()
    {
        return $this->origin;
    }

    /**
     * 批量设置数据
     * @param $data
     * @return $this
     */
    public function setData($data)
    {
        if (is_object($data)) {
            $data = get_object_vars($data);
        }
        $this->data = array_merge($this->data, $data);
        return $this;
    }

    protected static function init()
    {
        self::$config = $config = get_addon_config('cms');
        self::beforeInsert(function ($row) {
            if (!isset($row['admin_id']) || !$row['admin_id']) {
                $admin_id = session('admin.id');
                $row['admin_id'] = $admin_id ? $admin_id : 0;
            }

        });
        self::afterInsert(function ($row) {
            $pk = $row->getPk();

//            $channel = Channel::get($row['channel_id']);
//            $row->getQuery()->where($pk, $row[$pk])->update(['model_id' => $channel ? $channel['model_id'] : 0]);
            if (!$row['weigh']) {
                $max = self::max('weigh');
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $max]);
            }
        });
        self::beforeWrite(function ($row) {
            //在更新之前对数组进行处理
            foreach ($row->getData() as $k => $value) {
                if (is_array($value) && is_array(reset($value))) {
                    $value = json_encode(self::getArrayData($value), JSON_UNESCAPED_UNICODE);
                } else {
                    $value = is_array($value) ? implode(',', $value) : $value;
                }
                $row->setAttr($k, $value);
            }
            $changedData = $row->getChangedData();
            if (isset($changedData['flag'])) {
                $row['weigh'] = in_array('top', explode(',', $changedData['flag']))
                    ? ($row['weigh'] == 0 ? 9999 : $row['weigh'])
                    : ($row['weigh'] == 9999 ? 0 : $row['weigh']);
            }

//            if (isset($row['content']) && !self::$config['realtimereplacelink']) {
//                $row['content'] = Service::autolinks($row['content']);
//            }

            if (isset($changedData['publishtime']) && $changedData['publishtime'] > time() && $row['status'] == 'normal') {
                $row['status'] = 'prepare';
            }
        });
        self::afterWrite(function ($row) use ($config) {
    /*        if (isset($row['channel_id'])) {
                //在更新成功后刷新副表、TAGS表数据、栏目表
                $channel = Channel::get($row->channel_id);
                if ($channel) {
                    $model = Modelx::get($channel['model_id']);
                    if ($model) {
                        $values = array_intersect_key($row->getData(), array_flip($model->fields));
                        $values['id'] = $row['id'];
                        if (isset($row['content'])) {
                            $values['content'] = $row['content'];
                        }
                        if (isset($row['content_zh_cn'])) {
                            $values['content_zh_cn'] = $row['content_zh_cn'];
                        }
                        if (isset($row['content_en'])) {
                            $values['content_en'] = $row['content_en'];
                        }
                        //更新副表
                        $addonTbl = \think\Db::name($model['table']);
                        if ($addonTbl->find($row['id'])) {
                            $addonTbl->update($values);
                        } else {
                            $addonTbl->insert($values, true);
                        }
                    }
                }
            }*/
            if (isset($row['tags'])) {
                \addons\cms\model\Tag::refresh($row['tags'], $row['id']);
            }
//            $changedData = $row->getChangedData();
//            if (isset($changedData['status']) && $changedData['status'] == 'normal') {
//                if (isset($row['user_id']) && isset($config['score']['postarchives'])) {
//                    //增加积分
//                    User::score($config['score']['postarchives'], $row['user_id'], '发布文章');
//                }
//                //推送到熊掌号和百度站长
//                if ($config['baidupush']) {
//                    $urls = [$row->fullurl];
//                    \think\Hook::listen("baidupush", $urls);
//                }
//            }
//            if (isset($changedData['channel_id']) || isset($changedData['channel_ids'])) {
//                //有更新栏目和副栏目需要刷新统计数据
//                $refreshIds = [];
//                $originData = $row->getOriginData();
//                if (isset($changedData['channel_id'])) {
//                    $refreshIds = array_merge($refreshIds, [$originData['channel_id'] ?? 0, $changedData['channel_id']]);
//                }
//                if (isset($changedData['channel_ids'])) {
//                    $refreshIds = array_merge($refreshIds, explode(',', $originData['channel_ids'] ?? ''), explode(',', $changedData['channel_ids']));
//                }
//                $refreshIds = array_filter(array_unique($refreshIds));
//                Channel::refreshItems($refreshIds);
//            }
//            if ($config['searchtype'] == 'xunsearch') {
//                //更新全文搜索
//                FulltextSearch::update($row->id);
//            }
        });
        self::afterDelete(function ($row) use ($config) {
            $data = Archives::withTrashed()->find($row['id']);
            if ($data) {
                if ($row['status'] == 'normal' && isset($config['score']['postarchives'])) {
                    User::score(-$config['score']['postarchives'], $row['user_id'], '删除文章');
                }
//                if ($config['searchtype'] == 'xunsearch') {
//                    FulltextSearch::del($row);
//                }
            } else {
                //删除相关TAG
                \addons\cms\model\Tag::refresh('', $row['id']);
            }
            //删除评论
            Comment::deleteByType('archives', $row['id'], !$data);

            //删除收藏
            ArchivesInteraction::where('type','archives')->where('aid',$row['id'])->delete();

            //删除收藏
            Collection::where('type','archives')->where('aid',$row['id'])->delete();
            //刷新统计数据
//            $refreshIds = array_merge([$row['channel_id']], explode(',', $row['channel_ids']));
//            $refreshIds = array_filter(array_unique($refreshIds));
//            Channel::refreshItems($refreshIds);
        });
    }

    public function getFlagList()
    {
        $config = get_addon_config('cms');
        return $config['flagtype'];
    }

    public function getStatusList()
    {
        return [
            'normal' => __('Status Normal'),
            'hidden' => __('Status Hidden'),
/*            'draft' => __('Status Draft'),
            'prepare' => __('Status Prepare'),
            'rejected' => __('Status rejected'),
            'pulloff' => __('Status pulloff')*/
        ];
    }

    public function getStyleBoldAttr($value, $data)
    {
        return in_array('b', explode('|', $data['style']));
    }

    public function getStyleColorAttr($value, $data)
    {
        $result = preg_match("/(#([0-9a-z]{6}))/i", $data['style'], $matches);
        return $result ? $matches[1] : '';
    }

    public function getFlagTextAttr($value, $data)
    {
        $value = $value ? $value : $data['flag'];
        $valueArr = $value ? explode(',', $value) : [];
        $list = $this->getFlagList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }

    public function getPublishtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['publishtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPublishtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : ($value ? $value : null);
    }

    protected function setCreatetimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : ($value ? $value : null);
    }

    protected function setKeywordsAttr($value)
    {
        return str_replace(["&nbsp;", "\r\n", "\r", "\n"], "", strip_tags($value));
    }

    protected function setDescriptionAttr($value)
    {
        return str_replace(["&nbsp;", "\r\n", "\r", "\n"], "", strip_tags($value));
    }

    public static function getArrayData($data)
    {
        if (!isset($data['value'])) {
            $result = [];
            foreach ($data as $index => $datum) {
                $result['field'][$index] = $datum['key'];
                $result['value'][$index] = $datum['value'];
            }
            $data = $result;
        }
        $fieldarr = $valuearr = [];
        $field = $data['field'] ?? ($data['key'] ?? []);
        $value = $data['value'] ?? [];
        foreach ($field as $m => $n) {
            if ($n != '') {
                $fieldarr[] = $field[$m];
                $valuearr[] = $value[$m];
            }
        }
        return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
    }

    public function channel()
    {
        return $this->belongsTo('Channel', 'channel_id', '', [], 'LEFT')->setEagerlyType(0);
    }

    public function special()
    {
        return $this->belongsTo('Special', 'special_id', '', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 关联模型
     */
    public function model()
    {
        return $this->belongsTo("Modelx", 'model_id')->setEagerlyType(1);
    }

    /**
     * 关联模型
     */
    public function user()
    {
        return $this->belongsTo("\\app\\common\\model\\User", 'user_id', 'id', [], 'LEFT')->setEagerlyType(1);
    }

    /**
     * 关联模型
     */
    public function admin()
    {
        return $this->belongsTo("\\app\\admin\\model\\Admin", 'admin_id', 'id', [], 'LEFT')->setEagerlyType(1);
    }
}
